package com.examine.utils;

import java.security.MessageDigest;

public class MD5Helper {
	
	public static byte[] MD5Hash(String text){
		try {
			byte[] bytesOfMessage = text.getBytes("UTF-8");

			MessageDigest md5 = MessageDigest.getInstance("MD5");

			return md5.digest(bytesOfMessage);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static boolean MD5ValidateHash(String text, byte[] hash){
		try {
			byte[] bytesOfMessage = text.getBytes("UTF-8");

			MessageDigest md5 = MessageDigest.getInstance("MD5");

			byte[] generatedHash= md5.digest(bytesOfMessage);
			
			if(hash.length!=generatedHash.length)
				return false;
			
			for (int i = 0; i < generatedHash.length; i++) {
				if(generatedHash[i]!=hash[i])
					return false;
			}
			
			return true;
			
		} catch (Exception e) {
			return false;
		}
	}
}
